﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using CastleDefender.ConfigFolder;

namespace CastleDefender.Platform.ScreenBehavior
{
    public class TouchBehavior : DrawableGameComponent
    {

        #region VARIABLE

        protected Vector2 vecOldPosition1;
        protected Vector2 vecOldPosition2;
        protected Vector2 vecPosCurrent;
        protected Vector2 vecPosPress;
        protected Vector2 vecPosMove;
        protected Vector2 vecPosKeyUp;
        protected Vector2 vecConvertVector;
        protected bool blnIsPressed;
        protected bool blnIsKeyUp;
        protected bool blnIsMoved;
        protected bool blnIsZoomed;
        protected bool blnControlled;

        #endregion 

        #region CONSTRUCTOR

        public TouchBehavior(Game myGame):base (myGame)
        {
            vecOldPosition1 = new Vector2();
            vecOldPosition2 = new Vector2();
        }

        #endregion 

        #region PUBLIC METHOD

        public override void Update(GameTime gameTime)
        {
            TouchCollection touchCollection = TouchPanel.GetState();
            vecOldPosition1 = vecPosMove;
            if (touchCollection.Count == 1)
            {
                var touchLocation = touchCollection[0];
                //nếu như trước đó chưa press thì sẽ chuyển sang trạng thái pressed
                if (!blnIsPressed && touchLocation.State == TouchLocationState.Pressed)
                {
                    blnIsPressed = true;
                    blnIsKeyUp = false;
                    vecPosPress = touchLocation.Position;
                    vecPosCurrent = vecPosPress;
                } 
                //nếu trước đó là trạng thái Pressed thì sẽ chuyển sang Moved
                else if ((touchLocation.State == TouchLocationState.Moved && blnIsPressed)
                    && touchLocation.Position != vecPosPress)
                {
                    blnIsMoved = true;
                    blnIsKeyUp = false;
                    vecPosMove = touchLocation.Position;
                    vecPosCurrent = vecPosMove;
                }

                //Trả lại trạng thái false khi tay nhấc khỏi sự kiện phím.
                if (touchLocation.State == TouchLocationState.Released)
                {
                    blnIsMoved = false;
                    blnIsPressed = false;
                    blnIsKeyUp = true;
                    vecPosKeyUp = touchLocation.Position;
                    vecPosCurrent = vecPosKeyUp;
                    vecOldPosition1 = Vector2.Zero;
                    vecPosMove = Vector2.Zero;
                }
            }
                //Nếu có 2 chạm thì xác định đang zoom
            else if (touchCollection.Count == 2)
            {
                blnIsZoomed = true;
                blnIsPressed = false;
                blnIsMoved = false;

                vecOldPosition1 = touchCollection[0].Position;
                vecOldPosition2 = touchCollection[1].Position;
            }
            else
            {
                blnIsMoved = blnIsPressed = blnIsZoomed = false;
            }

            base.Update(gameTime);
        }

        public void SetConvertVector(Vector2 convertVector)
        {
            this.vecConvertVector = convertVector;
        }

        public Vector2 ConvertPosition(Vector2 position)
        {
            return position - vecConvertVector;
        }

        #endregion 

        #region PUBLIC PROPERTIES

        public Vector2 PosCurrent
        {
            get { return vecPosCurrent; }
            private set { }
        }

        public Vector2 PosPress
        {
            get { return ConvertPosition(vecPosPress); }
            private set{}
        }

        public Vector2 PosMove
        {
            get { return ConvertPosition(vecPosMove); }
            private set { }
        }

        public Vector2 PosKeyUp
        {
            get { return ConvertPosition(vecPosKeyUp); }
            private set { }
        }

        public bool IsPressed
        {
            get { return blnIsPressed; }
            private set { }
        }

        public bool IsMoved
        {
            get { return blnIsMoved; }
            private set { }
        }

        public bool IsKeyUp
        {
            get { return blnIsKeyUp; }
            private set { }
        }

        public bool IsZoomed
        {
            get { return blnIsZoomed; }
            private set { }
        }

        public bool IsControlled
        {
            get { return blnControlled; }
            set { blnControlled = value; }
        }

        public Vector2 Delta
        {
            get 
            {
                if (vecOldPosition1 == Vector2.Zero || vecPosMove == Vector2.Zero)
                    return Vector2.Zero;
                if (blnIsMoved)
                {
                    return vecOldPosition1 - vecPosMove;
                }
                return Vector2.Zero;
            }
            private set { }
        }

        #endregion 

    }
}
